package com.butterfly.rest.resource.restlet.server;


import com.butterfly.domain.entity.MerchantAd;
import com.butterfly.rest.resource.restlet.api.AdResources;
import com.butterfly.rest.resource.restlet.model.AdX;
import com.butterfly.service.MerchantAdService;
import org.apache.commons.codec.binary.Hex;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Deflater;

public class AdServerResources extends BaseResources implements AdResources {

    @Autowired
    MerchantAdService merchantAdService;

    @Override
    public List<AdX> getAdList() {

        List<MerchantAd> merchantAdList = merchantAdService.getMerchantAdList(page, pageSize);
        return convertToAdX(merchantAdList);

        //return getAdvertisementDummyList();
    }

    private List<AdX> convertToAdX(List<MerchantAd> merchantAdList) {
        List<AdX> adXList = new ArrayList<>();


        if (merchantAdList != null && merchantAdList.size() > 0) {
            for (MerchantAd merchantAd : merchantAdList) {
                AdX adx = new AdX();
                adx.setAdId(merchantAd.getId());
                adx.setAdDescription(merchantAd.getDescription());
                adx.setCategory(merchantAd.getCategoryType().getCategoryName());
                adx.setAdType(merchantAd.getAdType().ordinal());
                adx.setAdSize(merchantAd.getSize());
                adx.setAdContent(convertToHexString(compressImage(merchantAd.getData())));
                adXList.add(adx);
            }
        }
        return adXList;
    }

    private byte[] compressImage(byte[] image) {
        byte[] result = null;
        Deflater deflater = new Deflater();
        deflater.setLevel(Deflater.BEST_COMPRESSION);
        deflater.setInput(image);

        ByteArrayOutputStream bos = new ByteArrayOutputStream(image.length);
        deflater.finish();

        try {
            byte[] buff = new byte[1024];
            while (!deflater.finished()) {
                int count = deflater.deflate(buff);
                bos.write(buff, 0, count);
            }
            bos.close();
            result = bos.toByteArray();

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return result;
    }

    private String convertToHexString(byte[] image) {
        char[] hex = Hex.encodeHex(image);
        return new String(hex);
    }

    private List<AdX> getAdvertisementDummyList() {
        List<AdX> advertisementList = new ArrayList<>();
        AdX ad1 = new AdX();
        ad1.setAdId(1L);
        ad1.setAdDescription("Promo ADS");
        ad1.setAdContent("1F2B");
        ad1.setAdSize("100x200");
        ad1.setCategory("1");

        AdX ad2 = new AdX();
        ad2.setAdId(2L);
        ad2.setAdDescription("Promo RET");
        ad2.setAdContent("1F2C");
        ad2.setAdSize("20x200");
        ad2.setCategory("2");

        advertisementList.add(ad1);
        advertisementList.add(ad2);

        return advertisementList;
    }
}
